# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""

from django.contrib import admin

# Register your models here.
from apps.home.models import LackBus
from import_export.admin import ExportActionModelAdmin
from datetime import datetime
from django.utils.http import urlquote

from apps.home.resource import LackBusResource

admin.site.site_header = '佛山公交数据资产管理'  # 设置header
admin.site.site_title = '佛山公交数据资产管理'  # 设置title
admin.site.index_title = '佛山公交数据资产管理'


@admin.register(LackBus)
class LackBusAdmin(ExportActionModelAdmin, admin.ModelAdmin):
    # 指定资源
    resource_class = LackBusResource

    list_display = ('comp', 'op_date', 'seq', 'line', 'word_track', 'plate', 'bus_type', 'plate_v2', 'start_time',
                    'end_time', 'situation', 'station', 'lack_trip_num', 'sup_trip_num', 'total_lack_trip_num',
                    'lack_bus_start_time', 'sup_bus_start_time', 'submit_date', 'category', 'type')
    list_filter = ['op_date', 'comp', 'plate', 'category', 'type']
    search_fields = ['situation']
    date_hierarchy = 'op_date'
    list_per_page = 20

    # 自定义导出文件名
    def get_export_filename(self, request, queryset, file_format):
        filename = "非计划{}系统导出表.{}".format(datetime.now().date().strftime('%Y-%m-%d'), file_format.get_extension())
        # 否则中文文件名称会变成下载.xls
        filename = urlquote(filename)
        print(filename)
        return filename